/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.panels;

import com.david.documentation.domain.entities.tags.Tag;
import com.david.documentation.service.doku.DokuService;
import com.david.documentation.service.tag.TagService;
import com.david.documentation.web.view.components.DokuPagingNavigator;
import com.david.documentation.web.view.pages.TagPage;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class TagPanel extends Panel{

	@SpringBean
	private DokuService dokuService;

	public TagPanel(String id,final TagService tagService) {
		super(id);
		setOutputMarkupId(true);

		DataView dataView = new DataView<Tag>("view",new ListDataProvider(tagService.findAll())) {

			@Override
			protected void populateItem(Item item) {
				final Tag tag = (Tag)item.getModelObject();
				AjaxLazyLoadPanel countPanel = new AjaxLazyLoadPanel("count") {
					@Override
					public Component getLazyLoadComponent(String markupId) {
						return new Label(markupId,"("+tagService.getNumberOfDokus(tag.getId())+")");
					}
				};
				item.add(new BookmarkablePageLink("link", TagPage.class,new PageParameters("tag="+tag.getName()))
					.add(new Label("label",tag.getName())).add(countPanel));
			}

		};
		dataView.setItemsPerPage(80);
		dataView.setOutputMarkupId(true);
		add(new DokuPagingNavigator("navigator", dataView).setVisible(dataView.getPageCount()>1));
		add(dataView);
	}
}
